﻿using AMInstantMessagingDao;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace AMInstantMessagingServerAPI
{
    public static class SimpleCacheEx
    {
        /// <summary>
        /// 缓存客户端信息
        /// </summary>
        /// <param name="_SimpleCache"></param>
        /// <param name="key"></param>
        /// <param name="info"></param>
        public static void CacheClientInfoModel(this SimpleCache _SimpleCache, string token, UserClientInfo info)
        {
            var cache_key = _SimpleCache.GetCacheKey(typeof(UserClientInfo).FullName, token);
            _SimpleCache.ConnectionMultiplexer.GetDatabase().StringSet(cache_key, JsonConvert.SerializeObject(info));
        }

        /// <summary>
        /// 获取缓存的客户端信息
        /// </summary>
        /// <param name="_SimpleCache"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static UserClientInfo GetCacheClientInfoModel(this SimpleCache _SimpleCache, string token)
        {
            var cache_key = _SimpleCache.GetCacheKey(typeof(UserClientInfo).FullName, token);
            var value = _SimpleCache.ConnectionMultiplexer.GetDatabase().StringGet(cache_key);

            if (value.IsNull)
                return null;

            return JsonConvert.DeserializeObject<UserClientInfo>(value.ToString());
        }
    }
}
